package com.example.hospital.api.common;

import cn.hutool.http.HttpStatus;
import com.example.hospital.api.common.handler.BaseErrorInfoInterface;
import com.example.hospital.api.common.handler.ExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private String code;
    private String msg;
    private T data;

    public Result(){}

    public Result(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.msg = errorInfo.getResultMsg();
    }

    public static Result success() {
        return success(null);
    };

    public static <T> Result success(T data)
    {
        Result<T> result = new Result<T>();
        result.setCode(ExceptionEnum.SUCCESS.getResultCode());
        result.setMsg(ExceptionEnum.SUCCESS.getResultMsg());
        result.setData(data);
        return result;
    };

    /**
     * 失败
     */
    public static Result error(BaseErrorInfoInterface errorInfo) {
        Result rb = new Result();
        rb.setCode(errorInfo.getResultCode());
        rb.setMsg(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static Result error(String code, String message) {
        Result rb = new Result();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static Result error( String message) {
        Result rb = new Result();
        rb.setCode("-1");
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }

}
